﻿package BinarySearch;

/**
 * @ClassName LC875
 * @Description
 * @Author ZJX
 * @Date 2024/10/26 21:11
 * @Version 1.0
 */
public class LC875 {
    public static int minEatingSpeed(int[] piles, int h) {
        int left = 1;
        int right = 0;
//        获取二分上限
        for(int p : piles){
            right = Math.max(p,right);
        }

        int ret = 0;
        int mid = 0;
        while (left <= right){
            mid = left + ((right - left) >> 1); // 等同于 mid = left + (right - left) / 2
            if (getHour(piles,mid) > h){
                left = mid + 1;
            } else {
                ret = mid;
                right = mid - 1;
            }
        }
        return ret;
    }

    private static long getHour(int[] piles, int speed) {
        long ret = 0;
        for (int p : piles){
            ret  += (p + speed - 1) / speed;
        }
        return ret;
    }
}
